<?php
header("Content-Type: application/json; charset=utf-8");
$servername = "localhost";
$username = "";
$password = "";
$dbname = "";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
$conn->set_charset("utf8");

$data = file_get_contents("php://input");
$processinfo = json_decode($data, true);
$id = $processinfo['eventid'] ?? '';
$decision = $processinfo['decision'] ?? '';

if ($decision === 'pass') {
    $updateStmt = $conn->prepare("UPDATE event SET status = 1 WHERE eventid = ?");
    $updateStmt->bind_param("i", $id);
    $updateStmt->execute();
    $updateStmt->close();

    $selectStmt = $conn->prepare("SELECT title, starttime, endtime, space, type, `range`, status FROM event WHERE eventid = ?");
    $selectStmt->bind_param("i", $id);
    $selectStmt->execute();
    $result = $selectStmt->get_result();

    if ($result->num_rows > 0) {
        $row = $result->fetch_assoc();
        $title = $row['title'];
        $starttime = $row['starttime'];
        $endtime = $row['endtime'];
        $space = $row['space'];
        $type = $row['type'];
        $range = $row['range'];
        $status = $row['status'];
        $insertStmt = $conn->prepare("INSERT INTO show_event (title, starttime, endtime, space, type, `range`, status,hot) VALUES (?,?,?,?,?,?,?,0)");
        $insertStmt->bind_param("ssssssi", $title, $starttime, $endtime, $space, $type, $range, $status);
        $insertStmt->execute();
        $insertStmt->close();
    }

    $selectStmt->close();
}
if ($decision === 'delete') {
    $stmt = $conn->prepare("DELETE FROM event WHERE eventid = ?");
    $stmt->bind_param("i", $id);
    $stmt->execute();
    $stmt->close();
}

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
